package com.mbigger.admin.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.mbigger.admin.entity.AclModule;

@Mapper
public interface AclModuleMapper {
	@Insert("INSERT INTO sys_acl_module (name, parent_id, level, seq, status, remark, operator, operate_time, operate_ip) "
			+ "VALUES (#{name}, #{parentId}, #{level}, #{seq},  #{status}, #{remark}, #{operator}, #{operateTime}, #{operateIp})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	int insert(@Param("aclModule")AclModule aclModule);

	@Delete("DELETE FROM sys_acl_module WHERE id=#{id}")
	int deleteById(@Param("id")Integer id);

	@Update("UPDATE sys_acl_module name=#{name}, level=#{level}, seq=#{seq}, status=#{status}, remark=#{remark}, operator=#{operator}, operate_time=#{operateTime}, operate_ip=#{operateIp}")
	int updateById(@Param("aclModule")AclModule aclModule);

	@Select("SELECT * FROM sys_acl_module where id=#{id}")
	AclModule selectById(@Param("id")Integer id);

	@Select("SELECT * FROM sys_acl_module WHERE level LIKE #{level}")
	List<AclModule> getChildAclModuleListByLevel(@Param("level")String level);

	@Update("<script>"
			+ "<foreach collection='aclMoudleList' item='aclMoudle' open='(' close=')' separator=';'>"
			+ "UPDATE sys_acl_module SET level=#{aclMoudle.level} WHERE id=#{aclMoudle.id}"
			+ "</foreach>"
			+ "</script>")
	void batchUpdateLevel(@Param("aclMoudleList")List<AclModule> aclMoudleList);

	@Select("SELECT * FROM sys_acl_module")
	List<AclModule> getAllAclModule();

	@Select("<script>"
			+ "SELECT count(1) "
			+ "FROM sys_acl_module "
			+ "WHERE name=#{name}"
			+ "<if test='parentId != null'>"
			+ "AND parent_id!=#{parentId}"
			+ "</if>"
			+ "<if test='id != null'>"
			+ "AND id!=#{id}"
			+ "</if>"
			+ "</script>")
	int countByNameAndParentId(@Param("parentId")Integer parentId, @Param("name")String name,  @Param("id")Integer id);

	@Select("SELECT count(1) FROM sys_acl_module WHERE parent_id=#{aclModuleId}")
	int countByParentId( @Param("aclModuleId")int aclModuleId);
}
